#include "exception.h"

#include "compat.h"
#include "wstr.h"

#ifndef WIN32
#	include <errno.h>
#	include <string.h>
#endif

namespace util {

Exception::Exception(std::wstring errmsg) :
	errmsg(errmsg)
{}

std::wstring Exception::what() const {
	return errmsg;
}


std::wstring sysErrorMsg() {
	std::wstring last_error_str;
#ifdef WIN32
	LPWSTR errorstr_array = NULL;
	int errorstr_length = FormatMessageW(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
		0, WSAGetLastError(), 0,
		(LPWSTR)&errorstr_array,
		0, 0
	);
	errorstr_array[errorstr_length - 2] = 0;
	last_error_str = errorstr_array;
	LocalFree(errorstr_array);
#else
	last_error_str = wstr::toWide(strerror(errno));
#endif
	return last_error_str;
}

}
